package com.woniu.stream;

import com.woniu.entity.Account;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GatStream {
    public static void main(String[] args) {
        List<Account> list = Database.getList();
//        list.stream().filter(acc->acc.getAddress().equals("成都")).forEach(System.out::println);
//        Integer i = list.stream().map(acc -> acc.getMoney()).reduce(Integer::sum).get();
//        System.out.println(i);
//        list.stream().map(acc->acc.getAddress()).distinct().forEach(System.out::println);
//        list.stream().sorted(Account::compareTo).forEach(System.out::println);
//          list.stream().skip(2).limit(2).forEach(System.out::println);
//        long count = list.stream().filter(acc -> acc.getAddress().equals("成都")).count();
//        System.out.println(count);
//
//        Account max = list.stream().max((a1, a2) -> a1.getMoney() - a2.getMoney()).get();
//        System.out.println(max.getMoney());
//
//        Account min = list.stream().min((a1, a2) -> a1.getMoney() - a2.getMoney()).get();
//        System.out.println(min.getMoney());
//
//        Double avg = list.stream().collect(Collectors.averagingDouble(Account::getMoney));
//        System.out.println(avg);
//
//        double min1 = list.stream().collect(Collectors.summarizingDouble(Account::getMoney)).getMin();
//        System.out.println(min1);
//        double max1 = list.stream().collect(Collectors.summarizingDouble(Account::getMoney)).getMax();
//        System.out.println(max1);

//        Map<String, List<Account>> map = list.stream().collect(Collectors.groupingBy(Account::getAddress));
//        for(String key : map.keySet()){
//            System.out.println(key);
//            List<Account> accounts = map.get(key);
//            for(Account account : accounts){
//                System.out.println(account);
//            }
//        }
        List<Account> list1 = list.stream().filter(acc -> acc.getMoney() >= 3000 && acc.getMoney() <= 5000).collect(Collectors.toList());
        list1.forEach(System.out::println);
    }


    private static void extracted() {
        String[] strs = {"11","22", "33","44"};
//1. 通过Arrays.stream获取stream对象
        Stream<String> stream1 = Arrays.stream(strs);
        System.out.println(stream1);
//2. 通过集合(List,Set)创建Stream对象
        List<String> list = Arrays.asList("aa", "bb", "cc");
        Stream<String> stream2 = list.stream();
        System.out.println(stream2);
//3. 通过stream.of方法创建
        Stream<String> stream3 = Stream.of(  "aa", "bb","cc");
        System.out.println(stream3);
    }
}
